//
//  JHSelector.m
//  JHGJ1.0
//
//  Created by mac2 on 15/9/16.
//  Copyright (c) 2015年 com.yongchuangxianhe. All rights reserved.
//

#import "JHSelector.h"

@implementation JHSelector
{
    UIView *bgView;
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.frame = CGRectMake(0, SCREEN_HEIGHT-200, SCREEN_WIDTH, 200);
        self.backgroundColor = [UIColor groupTableViewBackgroundColor];
//        self.layer.cornerRadius = 5;
//        self.clipsToBounds = YES;
        
        
        
        UIDatePicker *datePicker = [[UIDatePicker alloc] init];
         datePicker.frame = CGRectMake(0, 50, self.frame.size.width, self.frame.size.height-50);
        //    设置样式
        [datePicker setDatePickerMode:UIDatePickerModeDate];
        
        [datePicker addTarget:self action:@selector(displayTime:) forControlEvents:UIControlEventValueChanged];
        //datePicker.center = self.center;
        
        [self addSubview:datePicker];
        NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//设置为中
        datePicker.locale = locale;
        NSDate *selected = [datePicker date];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSString *destDateString = [dateFormatter stringFromDate:selected];
        _dateString  = destDateString;

        
        _btn = [UIButton buttonWithType:UIButtonTypeCustom];
        _btn.backgroundColor = [UIColor groupTableViewBackgroundColor];
        _btn.frame = CGRectMake(0,0, 150, 40);
        [_btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        _btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        _btn.tag = 0;
        [_btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
          _btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
          _btn.titleEdgeInsets = UIEdgeInsetsMake(0,20, 0, 0);
        [self addSubview:_btn];
        
        UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 50)];
        img.image = [UIImage imageNamed:@"sr04"];
        [self addSubview:img];
        
        self.btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
        
        self.btn2.frame = CGRectMake(SCREEN_WIDTH-150, 0, 150, 40);
        [self.btn2 setTitleColor:ZTZThong_CORLOR forState:UIControlStateNormal];
        _btn2.tag = 1;
        _btn2.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self.btn2 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        _btn2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        _btn2.titleEdgeInsets = UIEdgeInsetsMake(0,0, 0, 20);
      
        
        [self addSubview:self.btn2];

 
        
    }
    
    return self;
}

- (void)displayTime:(UIDatePicker *)datePicker
{
    NSDate *date = datePicker.date;
    
    //    时间转换器
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //    设置时间的格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    //    把时间转换成字符串
    _dateString  = [dateFormatter stringFromDate:date];
    ////DBLOG(@"%@",_dateString);
}
- (void)show
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
   
    bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    bgView.backgroundColor = [UIColor blackColor];
    bgView.alpha = 0.5;
    
    [window addSubview:bgView];
    [window addSubview:self];
    
    
    UITapGestureRecognizer *recognizerTap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                    action:@selector(handleTapBehind:)];
    
    [recognizerTap setNumberOfTapsRequired:1];
    recognizerTap.cancelsTouchesInView = NO;
    [bgView addGestureRecognizer:recognizerTap];
}

- (void)handleTapBehind:(UITapGestureRecognizer *)sender{
    if (sender.state == UIGestureRecognizerStateEnded){
        CGPoint location = [sender locationInView:nil];
        
        if (![self pointInside:[self convertPoint:location fromView:self.window] withEvent:nil]){
            [bgView removeFromSuperview];
            [self removeFromSuperview];
            //[self dismissWithClickedButtonIndex:0 animated:YES];
        }
    }
}
- (void)btnClick:(UIButton *)btn
{
    ////DBLOG(@"haha");
    self.clickedBtnIndexBlock(btn.tag);
    
    [bgView removeFromSuperview];
    [self removeFromSuperview];
}

@end
